package com.sdkj.controller.finance;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.finance.FinanceWaterBillsService;
import com.sdkj.util.Const;
import com.sdkj.util.PageData;
import com.sdkj.util.ReadExcel;
import com.sdkj.util.Tools;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/** 
 * 类名称：FinanceWaterBillsController
 * 创建人：FH 
 * 创建时间：2017-06-01
 */
@Controller
@RequestMapping(value="/financewaterbills")
public class FinanceWaterBillsController extends BaseController {
	
	String menuUrl = "financewaterbills/list.do"; //菜单地址(权限用)
	@Resource(name="financewaterbillsService")
	private FinanceWaterBillsService financewaterbillsService;
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public ModelAndView save(@RequestParam(required=false) MultipartFile file,String type) throws Exception{
		ModelAndView mv = this.getModelAndView();
		logBefore(logger, "新增FinanceWaterBills");
		PageData pd = new PageData();
		String m_name_account="";
		String m_account_num="";
		String m_opening_bank="";
		String currency="";
//		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){
			try {
				if (null != file && !file.isEmpty()) {
					List<List<Object>> list = ReadExcel.readExcel(file,type);
					if ("0".equals(type)) {//建设银行
						for (List<Object> list2 : list) {
							if(list2==null||list2.size()==0) break;  //读取到空结束
							pd.put("m_account_num", list2.get(0).toString());	//我的账号		
							pd.put("m_name_account", list2.get(1).toString());	//我的账户名称	
							pd.put("m_opening_bank", m_opening_bank);//我的银行账户开户行
							pd.put("trading_time", list2.get(2).toString());//交易时间								
							pd.put("crebit_balance", list2.get(3).toString()==""?"0.00":list2.get(3).toString());//借方发生额（支取）
							pd.put("credit_balance", list2.get(4).toString()==""?"0.00":list2.get(4).toString());//贷方发生额（收入）
							pd.put("account_balance", list2.get(5).toString()==""?"0.00":list2.get(5).toString());	//余额					
							pd.put("currency", list2.get(6).toString());	//币种
							pd.put("name_account", list2.get(7).toString());	//对方户名			
							pd.put("account_number", list2.get(8).toString());	//对方账户						
							pd.put("opening_bank", list2.get(9).toString());//对方开户机构
							pd.put("summary", list2.get(11).toString());//摘要
							pd.put("remarks", list2.get(12).toString());//备注
							pd.put("serial_code", list2.get(13).toString());// 交易流水号
							pd.put("bank_type", "0");// 银行类型 0建设银行 1西安银行 2招商银行 3中信银行
							pd.put("creat_time",Tools.date2Str(new Date()));//创建时间
							financewaterbillsService.save(pd);
						}
					}else if("1".equals(type)){//西安银行
						for (List<Object> list2 : list) {
							if(list2==null||list2.size()==0) break;  //读取到空结束
							pd.put("trading_time", list2.get(0).toString());//交易时间								
							pd.put("crebit_balance", list2.get(1).toString().trim().equals("")?"0.00":list2.get(1).toString().replace("-","").replace(",",""));//借方发生额（支取）
							pd.put("credit_balance", list2.get(2).toString().trim().equals("")?"0.00":list2.get(2).toString().replace("+","").replace(",",""));//贷方发生额（收入）
							pd.put("account_balance", list2.get(4).toString().trim().equals("")?"0.00":list2.get(4).toString().replace(",",""));	//余额					
							pd.put("account_number", list2.get(5).toString());	//对方账户						
							pd.put("name_account", list2.get(6).toString());	//对方户名			
							pd.put("opening_bank", list2.get(7).toString());//对方开户机构
							pd.put("summary", list2.get(8).toString());//摘要
							pd.put("bank_type", "1");// 银行类型 0建设银行 1西安银行 2招商银行 3中信银行
							pd.put("creat_time",Tools.date2Str(new Date()));//创建时间
							financewaterbillsService.save(pd);
						}
					}else if("2".equals(type)){//招商银行
						int i = 0;
						for (List<Object> list2 : list) {
							if(list2==null||list2.size()==0) {
								i++; 
								continue;  //读取到空结束
							}else if(i==0) {
								m_name_account=list2.get(3).toString();  
								i++;
								continue;
							}else if(i==1) {
								m_account_num=list2.get(5).toString();  
								i++;
								continue;
							}else if(i==2) {
								currency=list2.get(1).toString();  
								i++;
								continue;
							}else  if(i==5) {
								m_opening_bank=list2.get(1).toString(); 
								i++;
								continue;
							}else if(i>=8) {
								pd.put("trading_time", list2.get(0).toString());//交易时间		
								pd.put("trading_day", list2.get(1).toString());//交易日	
								pd.put("trading_type", list2.get(3).toString());//交易日	
								pd.put("crebit_balance", list2.get(4).toString()==""?"0.00":list2.get(4).toString());//借方发生额（支取）
								pd.put("credit_balance", list2.get(5).toString()==""?"0.00":list2.get(5).toString());//贷方发生额（收入）
								pd.put("account_balance", list2.get(6).toString()==""?"0.00":list2.get(6).toString());	//余额					
								pd.put("summary", list2.get(7).toString());//摘要
								pd.put("serial_code", list2.get(8).toString());// 交易流水号
								pd.put("process_instance", list2.get(9).toString());// 流程实例号
								pd.put("business_name", list2.get(10).toString());// 业务名称
								pd.put("purpose", list2.get(11).toString());// 用途
								pd.put("business_reference_number", list2.get(12).toString());// 业务参考号
								pd.put("memo", list2.get(13).toString());// 业务摘要
								pd.put("name_account", list2.get(16).toString());	//对方户名			
								pd.put("account_number", list2.get(17).toString());	//对方账户						
								pd.put("opening_bank", list2.get(19).toString());//对方开户机构
								pd.put("bank_type", "2");// 银行类型 0建设银行 1西安银行 2招商银行 3中信银行
								pd.put("creat_time",Tools.date2Str(new Date()));//创建时间
								pd.put("m_name_account", m_name_account);	//我的账户名称	
								pd.put("m_account_num", m_account_num);	//我的账号		
								pd.put("m_opening_bank", m_opening_bank);//我的银行账户开户行
								pd.put("currency", currency);//我的银行账户开户行
								financewaterbillsService.save(pd);
								i++;
							}else{
								i++;
								continue;
							}
						}
					}else if("3".equals(type)){//中信银行
						int i = 0;
						for (List<Object> list2 : list) {
							if(list2==null||list2.size()==0) {
								i++; 
								continue;  //读取到空结束
							}else if(i==0) {
								String info=list2.get(2).toString();  
								m_account_num=info.substring(0, info.indexOf("-"));//我的账号	
								currency=info.substring(info.indexOf("-")+1,info.indexOf("-")+4);//币种
								m_name_account=info.substring(info.indexOf("-")+5);//我的账户名称
								i++;
								continue;
							}else if(i>=3) {
								pd.put("trading_time", list2.get(0).toString());//交易时间								
								pd.put("account_number", list2.get(1).toString());	//对方账号						
								pd.put("name_account", list2.get(2).toString());	//对方户名			
								pd.put("opening_bank", list2.get(3).toString());//对方开户机构
								pd.put("crebit_balance", list2.get(4).toString()==""?"0.00":list2.get(4).toString());//借方发生额（支取）
								pd.put("credit_balance", list2.get(5).toString()==""?"0.00":list2.get(5).toString());//贷方发生额（收入）
								pd.put("account_balance", list2.get(6).toString()==""?"0.00":list2.get(6).toString());	//余额					
								pd.put("summary", list2.get(7).toString());//摘要
								pd.put("remarks", list2.get(8).toString());//备注
								pd.put("bank_type", "3");// 银行类型 0建设银行 1西安银行 2招商银行 3中信银行
								pd.put("creat_time",Tools.date2Str(new Date()));//创建时间
								pd.put("m_name_account", m_name_account);	//我的账户名称	
								pd.put("m_account_num", m_account_num);	//我的账号		
								pd.put("m_opening_bank", m_opening_bank);//我的银行账户开户行
								pd.put("currency", currency);//币种
								financewaterbillsService.save(pd);
								i++;
							}else{
								i++;
								continue;
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
//		} //校验权限
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表FinanceWaterBills");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = financewaterbillsService.list(page);	//列出FinanceWaterBills列表
			mv.setViewName("finance/financewaterbills/financewaterbills_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	
	
	
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增FinanceWaterBills页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("finance/financewaterbills/financewaterbills_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	
	
	
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
